/* 
 * Enterprise Library Extensions - DI + ORM.
 * Copyright (C) 2006
 *
 * GNU General Public License
 *
 * This program is free software; you can redistribute 
 * it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 */
using System;

namespace ELE.EnterpriseLibrary.Naming
{
	/// <summary>
  /// This class represents the address of a communications end-point. It consists of a type that describes the communication mechanism and an address contents determined by an RefAddr subclass. 
  /// <br/><br/>For example, an address type could be "BSD Printer Address", which specifies that it is an address to be used with the BSD printing protocol. Its contents could be the machine name identifying the location of the printer server that understands this protocol. 
  /// <br/><br/>A RefAddr is contained within a Reference. 
  /// <br/><br/>RefAddr is an abstract class. Concrete implementations of it determine its synchronization properties. 
	/// </summary>
	[Serializable]
	public abstract class RefAddr
	{
    /// <summary>
    /// Contains the type of this address.
    /// </summary>
    protected  string addrType;
    /// <summary>
    /// Constructs a new instance of RefAddr using its address type.
    /// </summary>
    /// <param name="addrType">A non-null string describing the type of the address</param>
    protected  RefAddr(string addrType)
    {
      this.addrType = addrType;
    }
    /// <summary>
    /// Retrieves the address type of this address.
    /// </summary>
    public string AddrType
    {
      get { return addrType; }
    }
    /// <summary>
    /// Retrieves the contents of this address.
    /// </summary>
    public abstract object Content {get;}
    /// <summary>
    /// Determines whether obj is equal to this RefAddr. 
    /// <br/><br/>obj is equal to this RefAddr all of these conditions are true 
    /// <br/><br/><ul>
    /// <li>non-null </li>
    /// <li>instance of RefAddr</li>
    /// <li>obj has the same address type as this RefAddr (using String.compareTo()) </li>
    /// <li>both obj and this RefAddr's contents are null or they are equal (using the equals() test). </li></ul>
    /// </summary>
    /// <param name="obj">possibly null obj to check</param>
    /// <returns>true if obj is equal to this refaddr; false otherwise</returns>
    public override bool Equals(object obj)
    {
      if(obj != null && obj is RefAddr)
      {
        RefAddr objAddr = (RefAddr)obj;
        if(this.AddrType.CompareTo(objAddr.AddrType) == 0)
        {
          if((objAddr.Content == null && this.Content == null) ||
            objAddr.Content.Equals(this.Content))
          {
            return true;
          }
        }
      }
      return false;
    }
    /// <summary>
    /// Computes the hash code of this address using its address type and contents. The hash code is the sum of 
    /// the hash code of the address type and the hash code of the address contents.
    /// </summary>
    /// <returns>The hash code of this address as an int</returns>
    public override int GetHashCode()
    {
      int addrHash = 0;
      if(this.AddrType != null) addrHash = this.AddrType.GetHashCode();
      int contentHash = 0;
      if(this.Content != null) contentHash = this.Content.GetHashCode();
      return addrHash + contentHash;
    }
    /// <summary>
    /// Generates the string representation of this address. The string consists of the address's type and contents 
    /// with labels. This representation is intended for display only and not to be parsed
    /// </summary>
    /// <returns>The non-null string representation of this address.</returns>
    public override string ToString()
    {
      return "addrType=" + this.AddrType + ", content=" + this.Content.ToString();
    }


	}
}
